package com.text1;

import java.sql.*;


public class Cha {
    public static void main(String[] args) throws SQLException {
        Cha cha = new Cha();
        cha.selectById(5,"财经", "2016-09-16 14:42:58");
    }
    public void selectById(int id, String name,String createDate) throws SQLException {
        Connection connection=null;
        Statement statement=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password="123456";
            connection= DriverManager.getConnection(url,username,password);
            String sql="SELECT * FROM news_category WHERE id=? and name=? and createDate=?";
            //statement=connection.createStatement();//不带条件的查询
            //带条件的查询
            preparedStatement= connection.prepareStatement(sql);

            preparedStatement.setInt(1,id);
            preparedStatement.setString(2,name);
            preparedStatement.setString(3,createDate);

            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                int ids=resultSet.getInt("id");
                String name1=resultSet.getString("name");
                String date1 = resultSet.getString("createDate");

                System.out.println(ids+"-----"+name1 + "------" + date1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            resultSet.close();
            preparedStatement.close();
            connection.close();
        }
    }
}
